<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .con{
            width:300px;
            height:200px;
            border:1px solid red;
            margin:0 auto;
        }
        .manager{
            width:300px;
            margin:0 auto;
        }
    </style>
</head>
<body>
    <div class="con" id="con">
        <img src="./images/animal1.png" id="img1"/>
    </div>
    <div class="manager">
        <button id="add">添加节点</button>
        <button id="insert">向子节点前添加节点</button>
    </div> 
</body>
<script>
    var con = document.getElementById("con");
    var add = document.getElementById("add");
    var insert = document.getElementById("insert");
    var img1 = document.getElementById("img1");

    add.onclick = function(){
        //创建一个节点元素的DOM对象
        var el = document.createElement("img");
        //设置DOM对象的属性
        el.src = "./images/fruit1.png";
        //将新创建的节点DOM对象，添加到父节点中
        con.appendChild(el);
    }
    insert.onclick = function(){
        var el = document.createElement("img");
        el.src = "./images/fruit2.png";
        //在con中添加新元素，新元素位置在img1之前
        con.insertBefore(el,img1);
    }
</script>
</html>